//
//  WYCForgetController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCForgetController.h"
#import "UIButton+CountDown.h"
#import "UIViewController+HUD.h"
#import "RSAUtil.h"
@interface WYCForgetController ()
@property(nonatomic,strong)UITextField * usertexfield;
@property(nonnull,strong) UITextField * codefield;
@property(nonatomic,strong)UIButton  *codebutton;
@property(nonatomic,strong) UITextField * passtexfield ;
@property(nonatomic,strong)UITextField * Zpasstexfield;
@property(nonatomic,strong)NSString * ticket;
@end

@implementation WYCForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建界面
    [self setupCreat];
}

#pragma make ------>>界面创建
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 100, NavHeight+30, 200, 40);
    NavRigsterLabel.textColor = wycMain;
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:40];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"找回密码";
    [self.view addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(ScreenWidth-50, NavHeight, 33, 33);
    [self.view addSubview:backbutton];
    
    NSArray  *imageArr = @[@"icon_phone",@"icon_code",@"icon_password",@"icon_password"];
    NSArray  *titleArr = @[@"请输入手机号",@"请输入验证码",@"请输入密码",@"请再次输入密码"];
    
    for (int i = 0; i<imageArr.count; i++)
    {
        
        UIView * phoneView = [[UIView alloc] init];
        phoneView.frame = CGRectMake(40 , CGRectGetMaxY(NavRigsterLabel.frame) + 80 * px+60*i, ScreenWidth-80, 40);
        phoneView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:phoneView];
        //手机号
        UIImageView *noticeimageView = [[UIImageView alloc] init];
        noticeimageView.frame = CGRectMake(10, 8, 25, 25 );
        noticeimageView.image = [UIImage imageNamed:[imageArr objectAtIndex:i]];
        [phoneView addSubview:noticeimageView];
        
        UITextField * userTextField = [[UITextField alloc] init];
        userTextField.frame = CGRectMake(CGRectGetMaxX(noticeimageView.frame) + 40 * px,10, phoneView.width-60, 20);
        userTextField.backgroundColor = [UIColor clearColor];
        userTextField.placeholder = [titleArr objectAtIndex:i];
        userTextField.tintColor = wycMain;
        userTextField.font = [UIFont systemFontOfSize:15];
        [phoneView addSubview:userTextField];
        UIView  *lin =[[UIView alloc]init];
        lin.frame = CGRectMake(40, 39,phoneView.width-40, 1);
        lin.backgroundColor = WYCbgColor;
        [phoneView addSubview:lin];
        if (i==0) {
            userTextField.clearButtonMode=UITextFieldViewModeAlways;
            userTextField.keyboardType = UIKeyboardTypeNumberPad;

            _usertexfield = userTextField;
            
        }
        if (i== 1) {
            //获取验证码
            userTextField.keyboardType = UIKeyboardTypeNumberPad;

            UIButton * codebutton = [[UIButton alloc] init];
            codebutton.frame = CGRectMake(phoneView.width-100,10, 100, 20);
            [codebutton setTitleColor:wycMain forState:UIControlStateNormal];
            codebutton.titleLabel.font = [UIFont systemFontOfSize:SmallFont];
            [codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
            [codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [phoneView addSubview:codebutton];
            _codefield = userTextField;
            
        }
        if (i == 2) {
            userTextField.secureTextEntry = YES;
            UIButton * passtexfieldButton = [[UIButton alloc] init];
            passtexfieldButton.frame = CGRectMake(phoneView.width-50 , 0, 40, 40);
            [passtexfieldButton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
            [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
            [phoneView addSubview:passtexfieldButton];
            _codebutton = passtexfieldButton;
            _passtexfield = userTextField;
            
        }
        if (i == 3) {
            userTextField.secureTextEntry = YES;

            UIButton * passtexfieldButton = [[UIButton alloc] init];
            passtexfieldButton.frame = CGRectMake(phoneView.width-50 , 0, 40, 40);
            [passtexfieldButton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
            [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
            [phoneView addSubview:passtexfieldButton];
            _codebutton = passtexfieldButton;
            _Zpasstexfield = userTextField;
            
        }
        
    }
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(80 * px, 400, ScreenWidth-160*px, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"确认" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(findBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
}


#pragma make --------->>找回
- (void)findBtn{
    WYCLogFunc;
    
    // 手机号判断是否为空
    if(_usertexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        return;
    }
    //验证码判断是否为空
    if(_codefield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
        return;
    }
    //密码是否为空
    if(_passtexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    if(_Zpasstexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    if (![_passtexfield.text isEqualToString:self.Zpasstexfield.text]) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"两次密码不一致"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
    NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    
    params[@"ticket"] = _ticket;
    params[@"authcode"] = _codefield.text;
    params[@"newPwd"] = pas;
    
    WYCLog(@"找回密码%@",params);
    [HttpTool POST:safemodifyPasswordByForget params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        WYCLog(@"找回密码%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [self showToast:@"找回成功"];
            [self backClick];
        }else{
            [self showToast:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
}
#pragma make -------->>返回
- (void)backClick{
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}
#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
if (_usertexfield.text.length == 0) {
    NSString * showStr = @"手机号不能为空";
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
    return;
}else{
    [_codebutton startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    
//    NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,_usertexfield.text,forgetkey];
//    
//    [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSDictionary   * result = responseObject[@"result"];
//
//        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
//            _ticket = result[@"ticket"];
//
//            NSLog(@"ticket%@",_ticket);
//            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
//            
//        }else{
//            
//            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
//        }
//        
//    } fail:^(NSURLSessionDataTask *task, NSError *error) {
//        
//    }];
    
 }
}
@end
